Skip to content

Declarative way for setting MongoDB transaction options #4552

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

ykardziyaka
Copy link
Contributor

@ykardziyaka ykardziyaka commented Nov 11, 2023

Closes #1628

  • You have read the Spring Data contribution guidelines.
  • You use the code formatters provided here and have them applied to your changes. Don’t submit any formatting related changes.
  • You submit test cases (unit or integration tests) that back your changes.
  • You added yourself as author in the headers of the classes you touched. Amend the date range in the Apache license header if needed. For new types, add the license header (copy from another file and set the current year only).

After I read the issue description and thought about it a bit, I figured there's conceptually three possible approaches to implement requested functionality:

  1. By adding new attributes to @Transactional annotations like @Transactional(readPreference = PRIMARY)
  2. By introducing new MongoDb-specific annotation for managing transactions
  3. By re-using existing attributes of @Transactional

First option requires to add database-specific attributes to database-generic annotation. That doesn't sound right.

Second option can be implemented either by completely re-implementing/duplicating whole @Transactional functionality in Spring Data MongoDb or by altering @Transactional interceptor so that it can pass attributes of new annotation along with existing values. Even if that's possible, it will be too much effort. Also, splitting transaction configuration into two annotations kinda confusing.

So I implemented third option. The only suitable attribute in @Transactional to store MongoDb options is label. Spring docs suggest that:

Labels may serve a solely descriptive purpose or map to pre-defined transaction manager-specific options.

This PR adds possibility to add 4 labels that will be recognized as transaction options on per-@Transactional basis:

  • mongo:maxCommitTime={value}
  • mongo:readConcern={value}
  • mongo:readPreference={value}
  • mongo:writeConcern={value}

mongo:readConcern, mongo:readPreference, mongo:writeConcern values are converted from String to respective Mongo Client types using Mongo Client API methods.

mongo:maxCommitTime is converted from String using Duration.parse(String).

Example of usage:

  @Transactional(transactionManager = "txManager", label = { 
    "mongo:maxCommitTime=PT1M", 
    "mongo:readConcern=majority" ,
    "mongo:readPreference=primary" ,
    "mongo:writeConcern=acknowledged" ,
  })
  public Entity doSomeStuff(Entity entity) {
      //...
  }

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 11, 2023
@christophstrobl christophstrobl self-assigned this Nov 13, 2023
@ykardziyaka
Copy link
Contributor Author

Missed some formatting in tests, should be fine now

@christophstrobl
Copy link
Member

Thank you very much @ykardziyaka for taking the time to dig into this topic. I like the approach using the label attribute 👍
Since we're already pretty close to the 4.2.0 GA release, we intend to pick up this PR for the next development iteration.

@christophstrobl christophstrobl added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 14, 2023
@christophstrobl
Copy link
Member

Currently planned to be picked up for Milestone 2.
We're also thinking of adding means to customize the the label prefix so that one can move off the default mongo:

@christophstrobl
Copy link
Member

@ykardziyaka you may want to have a look at the current issue branch.

@mp911de mp911de changed the title Declarative way for setting MongoDB transaction options. Declarative way for setting MongoDB transaction options Mar 12, 2024
mp911de pushed a commit that referenced this pull request Mar 12, 2024
mp911de pushed a commit that referenced this pull request Mar 12, 2024
mp911de added a commit that referenced this pull request Mar 12, 2024
Tweak naming. Add Javadoc and documentation.

See #1628
Original pull request: #4552
@mp911de
Copy link
Member

mp911de commented Mar 12, 2024

Thank you for your contribution. That's merged and polished now.

@mp911de mp911de closed this Mar 12, 2024
@mp911de mp911de added this to the 4.3 M2 (2024.0.0) milestone Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Declarative way for setting MongoDB transaction options
4 participants